#!/usr/bin/env python
"""
clean_style.py
Defines clean-up functions for style attr

Copyright (C) 2007 Aurele Duda, contact@aurele-origami.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""

import simplestyle

initial_style = {
    "color":"black",
    "color-interpolation":"sRGB",
    "color-interpolation-filters":"linearRGB",
    "color-rendering":"auto",
    "display":"inline",
    "enable-background":"accumulate",
    "fill":"black",
    "fill-opacity":"1",
    "fill-rule":"non-zero",
    "font-family":"",
    "font-size":"12px",
    "font-stretch":"normal",
    "font-style":"normal",    
    "font-variant":"normal",
    "font-weight":"normal",
    "image-rendering":"auto",
    "line-height":"125%",
    "marker":"none",
    "marker-end":"none",
    "marker-mid":"none",
    "marker-start":"none",
    "opacity":"1",
    "overflow":"visible",
    "stroke":"none",
    "stroke-dasharray":"none",
    "stroke-dashoffset":"0",
    "stroke-linecap":"but",
    "stroke-linejoin":"miter",
    "stroke-miterlimit":"4",
    "stroke-opacity":"1",
    "stroke-width":"1",
    "visibility":"visible",
    "text-align":"start",
    "text-anchor":"start",
    "text-rendering":"auto",
    "writing-mode":"lr-tb",
}

def cleanStyle(node) :
    """
    remove initial SVG styles in style attributes, or the whole style attr if a class is set
    """
    if node.attrib.has_key("class") and node.attrib["class"] != "" and node.attrib.has_key("style") :
        #TODO define a function that parse the CSS stylesheet and remove only
        # from the style attr what is already in it.
        #TODO parse the styles attributes like stroke=""
        delStyle(node)
    elif node.attrib.has_key("style") :
        cleanInitialStyle(node)
def delStyle(node) :
    """
    delete style attr
    """
    del node.attrib["style"]
def cleanInitialStyle(node) :
    """
    delete styles set in style attr if it is the same as initial style in SVG specs
    """
    objectStyle = simplestyle.parseStyle(node.attrib["style"])
    newStyle = {}
    for i in xrange(len(objectStyle)) :
        style = objectStyle.popitem()            
        if initial_style.has_key(style[0]) :
            if style[1] != initial_style[style[0]] :
                newStyle[style[0]] = style[1]
        else : 
            raise ValueError, u"%s style isn't a valid CSS string available in SVG" % style[0]
    node.attrib["style"] = simplestyle.formatStyle(newStyle)